<?php
namespace app\user\controller;


class Index extends Base
{

    public function register()
    {
        if (request()->isPost()) {

            $account = input('post.username', '', 'text');
            $mobile = input('post.mobile', '', 'text');
            $email = input('post.email', '', 'text');
            $password = input('post.password', '', 'text');
            $re_password = input('post.repassword', '', 'text');
            $nickname = input('post.nickname', '', 'text');

            if ($password != $re_password) {
                $this->error('两次提交的密码不一致');
            }

            $userModel = model('user/User');
            $uid = $userModel->register($account, $email, $mobile, $nickname, $password);
            if ($uid > 0) {
                $this->success('注册成功~', url('login'));
            } else {
                $this->error('注册失败');
            }
        } else {
            return view('register');
        }
    }


    public function login()
    {

        if (is_login()) {
            $this->redirect('index/index/index');
        }

        if (request()->isPost()) {

            $referer = input('referer', '');
            $account = input('post.account', '', 'text');
            $password = input('post.password', '', 'text');
            $userModel = model('User');
            $uid = $userModel->login($account, $password);
            if ($uid > 0) {
                $userModel->doLogin($uid);
                $this->success('登录成功~', $referer ?: url('index/index/index'));
            }
        } else {
            $refer = input('refer', '0');
            $this->assign('refer', $refer);
            return view('login');
        }
    }

    public function logout()
    {
        model('User')->doLogout();
        $this->redirect('index/index/index');
    }


}
